package com.wclass.controller.sys;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wclass.common.ReturnObject;
import com.wclass.entity.BaseNotice;
import com.wclass.interceptor.IgnoreAuth;
import com.wclass.service.IBaseNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户 后台控制器
 * </p>
 *
 * @author Bruce
 * @since 2019-01-11
 */
@RestController
@RequestMapping("/sysNotice")
public class SysNoticeController {
    @Autowired
    private IBaseNoticeService iBaseNoticeService;


    /**
     * 获取公告列表
     *
     * @param limit
     * @param offset
     * @return
     */
    @IgnoreAuth
    @RequestMapping(value = "/getNoticeList", method = RequestMethod.GET)
    public ReturnObject<List<BaseNotice>> getNoticeList(
            @ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
            @ApiParam("页数") @RequestParam(defaultValue = "1") int offset) {

        try {
            EntityWrapper<BaseNotice> ew = new EntityWrapper<BaseNotice>();
            ew.orderBy("crt_time", false);
            Page<BaseNotice> obj = iBaseNoticeService.selectPage(new Page<>(offset, limit), ew);
            return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());

        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject<>(12, "系统偷懒了");
        }
    }
    /**
     * 创建通知
     * @return
     */
    @IgnoreAuth
    @RequestMapping(value = "/createNotice", method = RequestMethod.GET)
    public ReturnObject createNotice( @ApiParam("通知标题") @RequestParam String title,
            @ApiParam("通知内容") @RequestParam String content,@ApiParam("接收对象") @RequestParam Integer type) {
        try {

            BaseNotice baseNotice = new BaseNotice();
            baseNotice.setReadNum(0);
            baseNotice.setContent(content);
            baseNotice.setTitle(title);
            baseNotice.setType(type);
            baseNotice.setCrtTime(new Date());
            if (iBaseNoticeService.insert(baseNotice) ){
                return new ReturnObject(0, "创建成功");
            }else{

                return new ReturnObject(1, "创建失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject(12, "系统偷懒了");
        }
    }




    /**
     * 删除通知
     * @param id
     * @return
     */
    @IgnoreAuth
    @RequestMapping(value = "/deleteNotice", method = RequestMethod.GET)
    public ReturnObject deleteNotice( @ApiParam("通知id") @RequestParam Long id) {
        try {
            BaseNotice baseNotice = iBaseNoticeService.selectOne(new EntityWrapper<BaseNotice>().eq("id",
                    id));
            if (iBaseNoticeService.deleteById(id) ){
                    return new ReturnObject(0, "删除成功");
            }else{

                return new ReturnObject(1, "删除失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject(12, "系统偷懒了");
        }
    }

    /**
     * 修改通知
     * @param id
     * @param content
     * @return
     */
    @IgnoreAuth
    @RequestMapping(value = "/updateNotice", method = RequestMethod.GET)
    public ReturnObject updateNotice( @ApiParam("通知id") @RequestParam Long id,
                                      @ApiParam("标题") @RequestParam String title,
                                      @ApiParam("通知内容") @RequestParam String content,
                                      @ApiParam("接收对象") @RequestParam Integer type) {
        try {
            BaseNotice baseNotice = iBaseNoticeService.selectOne(new EntityWrapper<BaseNotice>().eq("id",
                    id));
            baseNotice.setContent(content);
            baseNotice.setTitle(title);
            baseNotice.setCrtTime(new Date());
            baseNotice.setType(type);
            baseNotice.setReadNum(0);
            if (iBaseNoticeService.updateById(baseNotice)) {
                return new ReturnObject(0, "更新成功");
            } else {

                return new ReturnObject(1, "更新失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject(12, "系统偷懒了");
        }
    }



    /**
     * 置顶通知
     * @param id
     * @return
     */
    @IgnoreAuth
    @RequestMapping(value = "/roofPlacement", method = RequestMethod.GET)
    public ReturnObject roofPlacement( @ApiParam("通知id") @RequestParam Long id) {
        try {
            BaseNotice baseNotice = iBaseNoticeService.selectOne(new EntityWrapper<BaseNotice>().eq("id",
                    id));
            baseNotice.setCrtTime(new Date());
            if (iBaseNoticeService.updateById(baseNotice)) {
                return new ReturnObject(0, "更新成功");
            } else {

                return new ReturnObject(1, "更新失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject(12, "系统偷懒了");
        }

    }

}

